package com.donleo.security.controller;


import com.donleo.common.api.CommonResult;
import com.donleo.common.model.Role;
import com.donleo.security.service.RoleService;
import com.donleo.security.service.UserRoleRelationService;
import com.donleo.security.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author liangd
 * @since 2020-12-21
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private UserRoleRelationService userRoleRelationService;

    /**
     * 增加
     */
    @PostMapping("/add")
    public CommonResult add(@RequestBody Role role) {
        return roleService.add(role);
    }

    /**
     * 删除
     */
    @GetMapping("/delete")
    public CommonResult delete(Integer id) {
        return roleService.delete(id);
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public CommonResult update(@RequestBody Role role) {
        Assert.notNull(role.getId(), "Id is not to be null");
        return roleService.updateRole(role);
    }

    /**
     * 单个查询
     */
    @GetMapping("/findById")
    public CommonResult findById(Integer id) {
        Role role = roleService.findById(id);
        return CommonResult.success(role);
    }

    /**
     * 模糊查询
     */
    @GetMapping("/findAll")
    public CommonResult findAll(RoleVo roleVo) {
        List<Role> list = roleService.findAll(roleVo);
        long total = roleService.getCount(roleVo);
        return CommonResult.success(total, list);
    }

    /**
     * 查询所有
     */
    @GetMapping("/findRoleList")
    public CommonResult findRoleList() {
        List<Role> list = roleService.list();
        return CommonResult.success(list);
    }

}
